package cn.qyd.kaoshi;

import java.util.*;

/**
 * @author 邱运铎
 * @date 2024-03-20 22:13
 */
public class Demo04 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            int nums = Integer.parseInt(in.nextLine());
            boolean isReverse = "0".equals(in.nextLine()) ? true : false;
            HashMap<Integer,String> map = new HashMap<>();
            int[][] items = new int[nums][2];
            for (int i = 0; i < nums; i++) {
                String str = in.nextLine();
                String[] strs= str.split(" ");
                items[i][0] = i;
                items[i][1] = Integer.parseInt(strs[1]);
                map.put(i,strs[0]);
            }
            Arrays.sort(items,(item1,item2) -> {
                if (isReverse) {
                    return item2[1] - item1[1];
                } else {
                    return item1[1] - item2[1];
                }
            });
            for (int i = 0; i < nums; i++) {
                System.out.println(map.get(items[i][0]) + " " + items[i][1]);
            }
        }
    }

}
